<?php

// ... Initialize directories, load Smarty, connect to database, etc.

// If the PEAR root directory isn't automatically checked in the include_path,
// point this script directly to the location of the PEAR calendar directory
if ( !@include 'Calendar/Calendar.php' ) { 
	define('CALENDAR_ROOT','/usr/share/pear/Calendar/'); 
} 
// load these components of Pear Calendar
require_once CALENDAR_ROOT.'Month.php';
require_once CALENDAR_ROOT.'Day.php'; 
require_once CALENDAR_ROOT.'Month/Weekdays.php';
require_once CALENDAR_ROOT.'Decorator.php';
require_once CALENDAR_ROOT.'Decorator/Textual.php';
require_once CALENDAR_ROOT.'Decorator/Uri.php';
require_once CALENDAR_ROOT.'Hour.php';
require_once CALENDAR_ROOT.'Minute.php';

//var_dump('helping');

		// Decorator to "attach" functionality to selected hours
	class DiaryEvent extends Calendar_Decorator {
		var $entry;
		function DiaryEvent($calendar) {
			Calendar_Decorator::Calendar_Decorator($calendar);
		}
		function setEntry($entry) {
			$this->entry = $entry;
		}
		function getEntry() {
			return $this->entry;
		}
	}//end diaryevent
			
class DiaryEvents extends Calendar_Decorator { 
    var $entries = array();
     
    function addEntry($entry) { 
        $this->entries[] = $entry; 
    } 

    function getEntry() { 
        $entry = each($this->entries); 
        if ($entry) { 
            return $entry['value']; 
        } else { 
            reset($this->entries); 
            return false; 
        } 
    }
    function entryCount() {
        return count($this->entries);
    }
} 

class MonthPayload_Decorator2 extends Calendar_Decorator {
    function build($events=array()) { 
		//var_dump('MPD2');
// comment the following line out if you want to call "$month->build($selectedDays);" below!! Otherwise you'll get strange results!
        parent::build(); 
        foreach ($this->calendar->children as $i=> $child) { 
            $this->calendar->children[$i] = &new DiaryEvents($this->calendar->children[$i]);
        }
		//var_dump($events);
		//var_dump('count');
        if (count($events) > 0) {
            $this->setSelection($events); 
        }
        return true; 
    }
    function setSelection($events) {
		//var_dump('set Selection');
        foreach ($this->calendar->children as $i=> $child) {             
            //FIXME: find the bug in the PEAR Calendar that's cause the thisMonth() to return prev month value on day 1
            if($child->thisDay() === 1){
            	$stamp1 = $this->calendar->cE->dateToStamp( $child->thisYear(), $child->nextMonth(), $child->thisDay());
            	$stamp2 = $this->calendar->cE->dateToStamp($child->thisYear(), $child->nextMonth(), $child->nextDay());
            }else{
            	$stamp1 = $this->calendar->cE->dateToStamp( $child->thisYear(), $child->thisMonth(), $child->thisDay());
				$stamp2 = $this->calendar->cE->dateToStamp($child->thisYear(), $child->thisMonth(), $child->nextDay());
            }
            
            foreach ($events as $event) {
				$start = $this->time_converter($event, 'start');
				$end = $this->time_converter($event, 'end');
				
				
                if(
                	( ($stamp1 <= $start) && ($end === null) && ($stamp2 >= $start) ) || //on that day only
                    ( ($end !== null) && ($stamp1 <= $end) && ($stamp2 >= $end) ) || //ends on this day
                    ( ($end !== null) && ($stamp1 <= $start) &&  ($stamp2 >= $start) ) //|| //starts on this day
                   )
                {
                	if ( $stamp1 > $start ){//will add special text to show that the event is 'continuing'
                		$text = date('r',$start).' ; '.date('r',$stamp1).' ; '.date('r',$stamp2);
                		var_dump($text);
                		$new_event = clone $event;	
                		$new_event->special_strtime = "con't";
                		$this->calendar->children[$i]->addEntry($new_event);
                	}else{
                		$this->calendar->children[$i]->addEntry($event);
                	}
                    $this->calendar->children[$i]->setSelected();
                //}elseif( ( ($end !== null) && ($stamp1 > $start) && ($stamp2 < $end)  ) ){
                	
                	//$this->calendar->children[$i]->addEntry($text);
                	//$this->calendar->children[$i]->setSelected();
                }//end if
            }//end foreach
        }//end foreach
    }//end setSelection mehtod
	
	function time_converter($event, $task){
		switch($task){
			case 'start':
				$etime = ($event->times)? $event->times: '00:00:00';
			//	$start = $event->start_date.' '.$event->start_time;
				$start = $event->dates.' '.$etime;
				list($date, $time) = explode(' ', $start);
 	 		    list($year, $month, $day) = explode('-', $date);
   				list($hour, $minute, $second) = explode(':', $time);
    		    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
    		    return $timestamp;
				break;
				
			case 'end':				
				if(empty($event->enddates) && empty($event->endtimes)){
					//end not set treat as end of day or nullify
					return null;					
				}elseif(empty($event->enddates) && !empty($event->endtimes)){
					//day not set, treat as same as start day
					// validation should let us get here
					$end = $event->dates.' '.$$event->endtime;
					list($date, $time) = explode(' ', $end);
	 	 		    list($year, $month, $day) = explode('-', $date);
	   				list($hour, $minute, $second) = explode(':', $time);
	    		    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
	    		    return $timestamp;	
				}elseif(!empty($event->enddates) && !empty($event->endtimes)){
					//we have specific end day-time
					$end = $event->enddates.' '.$event->endtimes;
					list($date, $time) = explode(' ', $end);
	 	 		    list($year, $month, $day) = explode('-', $date);
	   				list($hour, $minute, $second) = explode(':', $time);
	    		    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
	    		    return $timestamp;
				}elseif(!empty($event->enddates) && empty($event->endtimes)){
					//we have specific end day but no time
					$end = $event->enddates.' '.'00:00:00';
					list($date, $time) = explode(' ', $end);
	 	 		    list($year, $month, $day) = explode('-', $date);
	   				list($hour, $minute, $second) = explode(':', $time);
	    		    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
	    		    return $timestamp;
				}else{
					return null;
				}
							
				break;
		
		}
	}
}//edn class

?>